/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package doan.quanlycanbo.portlet.service.impl;

import java.util.List;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.ResourceConstants;

import doan.quanlycanbo.portlet.NoSuchcapuyException;
import doan.quanlycanbo.portlet.service.base.capuyLocalServiceBaseImpl;
import doan.quanlycanbo.portlet.model.capuy;

/**
 *
 * @author Nguyen Tuan Vinh
 */
public class capuyLocalServiceImpl extends capuyLocalServiceBaseImpl {
	
	public List<capuy> getAll() throws SystemException{
		return capuyPersistence.findAll();
	}
	
	public capuy addCapUy(capuy capuy1, long userId) throws SystemException{
		long macapuy = CounterLocalServiceUtil.increment(capuy.class.getName());
		capuy capuy2 = capuyPersistence.create(macapuy);
		capuy2.setGroupId(capuy1.getGroupId());
		capuy2.setCompanyId(capuy1.getCompanyId());
		capuy2.setLanguageId(capuy1.getLanguageId());
		capuy2.setTencapuy(capuy1.getTencapuy());
		
		try {
			resourceLocalService.addResources(capuy1.getCompanyId(), capuy1.getGroupId(), userId, 
					capuy.class.getName(), capuy2.getPrimaryKey(), false, true, true);
		} catch (PortalException e) {
			e.printStackTrace();
		}
		
		return capuyPersistence.update(capuy2, false);
	}
	
	public capuy updateCapUy(long macapuy, String tencapuy) throws SystemException, NoSuchcapuyException{
		capuy capuy = capuyPersistence.findByPrimaryKey(macapuy);
		capuy.setTencapuy(tencapuy);
		return capuyPersistence.update(capuy, false);
	}
	
	public capuy deleteCapUy(long macapuy) throws SystemException, NoSuchcapuyException, PortalException{
		capuy capUy = capuyPersistence.findByPrimaryKey(macapuy);
		return deleteCapUy(capUy);
	}
	
	public capuy deleteCapUy(capuy capuy) throws SystemException, PortalException{
		resourceLocalService.deleteResource(capuy.getCompanyId(), capuy.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, capuy.getPrimaryKey());
		return capuyPersistence.remove(capuy);
	}
	
}